package com.ymm.lulutong.controller;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperWalletDomainService;
import com.ymm.lulutong.entity.Shipper;
import com.ymm.lulutong.entity.Wallet;
import com.ymm.lulutong.entity.WalletOpsLog;
import com.ymm.lulutong.utils.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/wallet")
public class ShipperWalletController {
    @Resource
    private ShipperWalletDomainService shipperWalletDomainService;

    /**
     * 获取钱包
     *
     * @param shipperId 货主id
     * @return 余额
     */
    @GetMapping("/getWallet")
    Result<Wallet> getWalletByShipperId(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        return shipperWalletDomainService.getWalletByShipperId(shipperId);
    }

    /**
     * 充值
     *
     * @param shipperId 货主id
     * @param topUpMoney   新增余额
     */
    @PostMapping("/topUp")
    Result<Long> topUp(Long shipperId, Integer topUpMoney) {
        if (Objects.isNull(shipperId) || Objects.isNull(topUpMoney)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        try {
            return shipperWalletDomainService.topUp(shipperId, topUpMoney);
        } catch (Exception e){
            e.printStackTrace();
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    /**
     * 提现
     *
     * @param shipperId       货主Id
     * @param withdrawalMoney 提现金额
     * @return 货主实体
     */
    @PostMapping("/withdrawal")
    public Result<Long> withdrawal(Long shipperId, Integer withdrawalMoney) {
        if (Objects.isNull(shipperId) || Objects.isNull(withdrawalMoney)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        try {
            return shipperWalletDomainService.withdrawal(shipperId, withdrawalMoney);
        } catch (Exception e){
            e.printStackTrace();
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    @PostMapping("/payForCargo")
    public Result<Long> payForCargo(Long shipperId, Integer payMoney, Long cargoId){
        if (Objects.isNull(shipperId) || Objects.isNull(payMoney) || Objects.isNull(cargoId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        try {
            return shipperWalletDomainService.payForCargo(shipperId, payMoney, cargoId);
        } catch (Exception e){
            e.printStackTrace();
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), "系统繁忙");
        }
    }

    /**
     * 获取账单流水
     *
     * @param shipperId 货主Id
     * @return 全部账单
     */
    @PostMapping("/getRunningAccount")
    public Result<List<WalletOpsLog>> getRunningAccount(Long shipperId){
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        return shipperWalletDomainService.getRunningAccountByShipperId(shipperId);
    }
}
